package com.transcoachapp;

import java.util.ArrayList;

import com.transcoachapp.adapter.RailAdapter;
import com.transcoachapp.data.Rail;
import com.transcoachapp.database.DataBaseHelper;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;

public class RailFragment extends ListFragment {

	
	private EditText edit;
	public static ArrayList<Rail> railList;
	public static DataBaseHelper db;
	private RailAdapter adapter;
	ViewPager mViewPager;
	
	 public static RailFragment newInstance() {
	        return new RailFragment();
	 }
	 @Override
	 public void onSaveInstanceState(Bundle outState) {
	        super.onSaveInstanceState(outState);
	        setUserVisibleHint(true);
	 }
	 @Override 
	 public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = (LinearLayout)inflater.inflate(R.layout.activity_rail_fragment,null);
		db=new DataBaseHelper(this.getActivity().getApplicationContext());
		railList=db.getRailList("");
		
		edit = (EditText) v.findViewById(R.id.railEdit);
		edit.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				railList=db.getRailList(s.toString());
				refresh();
				// TODO Auto-generated method stub
				///////edit this part/////////////
			}
		});
		adapter =new RailAdapter(getActivity(), R.layout.activity_rail_fragment, railList);		
		setListAdapter(adapter);
	    
		return	v;
	}
	public void refresh() {
		// TODO Auto-generated method stub
		if(railList!=null){
			adapter =new RailAdapter(getActivity(), R.layout.activity_rail_fragment, railList);		
			setListAdapter(adapter);
			adapter.notifyDataSetChanged();
		}
	}
}
